#include <chrono>
#include <functional>
#include <memory>
#include <string>

#include "rclcpp/rclcpp.hpp"
#include "common_lib/file/file_handle.hpp"
#include "common_lib/sqlite3/smart_sqlite3.hpp"
#include "common_lib/glog/glog.h"

class TestSqlite : public rclcpp::Node{
    public:
        TestSqlite();
        ~TestSqlite() = default;

    private:
        std::shared_ptr<SmartSqlite3> sqlite_ptr_;
};

TestSqlite::TestSqlite(): Node("test_sqlite3_node"){
    // 初始化文件信息
    string db_file = "/home/xxty/test.db";
    common::file::deleteFile(db_file);
    sqlite_ptr_ = std::make_shared<SmartSqlite3>(db_file);

    // 创建数据库
    string sql_cmd = "CREATE TABLE IF NOT EXISTS TEST (    \
        id             INTEGER     PRIMARY KEY     NOT NULL, \
        name           TEXT        NOT NULL, \
        data           REAL        NOT NULL, \
        time           DATETIME    NOT NULL \
    )";
    // sqlite_ptr_->execute_sql(sql_cmd, std::make_tuple());
    sqlite_ptr_->execute_sql_no_params(sql_cmd);

    // 查询数据库表信息
    sql_cmd = "PRAGMA table_info('TEST')";
    // json data = sqlite_ptr_->execute_sql(sql_cmd, std::make_tuple());
    json data = sqlite_ptr_->execute_sql_no_params(sql_cmd);
    cout << "data: " << data << endl;

    // 插入数据
    for(int i=0; i<10; ++i){
        sql_cmd = "INSERT INTO TEST (id, name, data, time) VALUES (?, ?, ?, ?);";
        data = sqlite_ptr_->execute_sql(sql_cmd, 
            std::forward_as_tuple(i, to_string(i), i/10.0, string("2025-05-07 18:43:16")));
    }
    
    // 查询数据i
    sql_cmd = "SELECT * FROM TEST WHERE data > ?";
    data = sqlite_ptr_->execute_sql(sql_cmd, std::make_tuple(0.5));
    cout << "data: " << data << endl;
}

int main(int argc, char * argv[]){
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<TestSqlite>());
    rclcpp::shutdown();
    return 0;
}